/*****************************************************************************/
/**
 *  @file   Util.h
 *  @author Naohisa Sakamoto
 */
/*----------------------------------------------------------------------------
 *
 *  Copyright (c) Visualization Laboratory, Kyoto University.
 *  All rights reserved.
 *  See http://www.viz.media.kyoto-u.ac.jp/kvs/copyright/ for details.
 *
 *  $Id$
 */
/*****************************************************************************/
#pragma once
#include <string>
#include <sstream>
#include <kvs/Vector3>
#include <kvs/Math>
#include <kvs/String>


namespace Util
{

inline kvs::Vec3 ToVec3( const kvs::Vector3d& v )
{
    return kvs::Vec3( (float)v.x(), (float)v.y(), (float)v.z() );
}

inline kvs::Vec3 Min( const kvs::Vec3& a, const kvs::Vec3& b )
{
    return kvs::Vec3(
        kvs::Math::Min( a[0], b[0] ),
        kvs::Math::Min( a[1], b[1] ),
        kvs::Math::Min( a[2], b[2] ) );
}

inline kvs::Vec3 Max( const kvs::Vec3& a, const kvs::Vec3& b )
{
    return kvs::Vec3(
        kvs::Math::Max( a[0], b[0] ),
        kvs::Math::Max( a[1], b[1] ),
        kvs::Math::Max( a[2], b[2] ) );
}

template <typename T>
inline std::string NumberToString( const T& number )
{
    return kvs::String::ToString<T>( number );
}

inline std::string DataSizeWithUnit( const size_t size )
{
    float memory_size = static_cast<float>( size );
    std::string unit = "";
    if ( memory_size >= 1024.0f ) { memory_size /= 1024.0f; unit = "K"; }
    if ( memory_size >= 1024.0f ) { memory_size /= 1024.0f; unit = "M"; }
    if ( memory_size >= 1024.0f ) { memory_size /= 1024.0f; unit = "G"; }
    if ( memory_size >= 1024.0f ) { memory_size /= 1024.0f; unit = "T"; }
    return NumberToString( memory_size ) + " " + unit + "bytes";
}

inline std::string NumberWithCommas( const size_t value )
{
    size_t n = value;
    size_t counter = 0;
    std::string s;
    do
    {
        s.insert( 0, 1, char( '0' + n  % 10 ) );
        n /= 10;
        if ( ++counter == 3 && n )
        {
            s.insert( 0, 1, ',' );
            counter = 0;
        }
    } while (n);

    return s;
}

} // end of namespace Util
